package com.tina.identify.controller;

import com.tina.identify.api.IdentifyApi;
import com.tina.identify.enu.UcloudEnum;
import com.tina.identify.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author tina
 * @Time 2020/12/30 上午10:11
 * @Version 1.0
 * @Content
 **/
@RestController
@Slf4j
public class HomeController {

    @Autowired
    private IdentifyApi identifyApi;

    @GetMapping("imgUrlCheck")
    public String imgUrlCheck(String url) {
        if (StringUtils.isEmpty(url)) {
            return "请上传在线图片";
        }
        if (!CommonUtil.isURL(url)) {
            return "在线图片路径不正确";
        }
        if (!CommonUtil.standByImg(url)) {
            return "不支持此格式图片";
        }
        String checkPorn = identifyApi.checkPorn(url);
        log.info("checkPorn:{}", checkPorn);
        return checkResult(checkPorn);
    }

    /**
     * 校验鉴定结果
     *
     * @param checkPorn
     * @return
     */
    private String checkResult(String checkPorn) {
        UcloudEnum ucloudEnum = UcloudEnum.valueOf(checkPorn);
        log.info("ucloudEnum:{}",ucloudEnum);
        switch (ucloudEnum) {
            case PASS:
                return UcloudEnum.PASS.getMsg();
            case CHECK:
                return UcloudEnum.CHECK.getMsg();
            case FORBID:
                return UcloudEnum.FORBID.getMsg();
        }
        return UcloudEnum.NO.getMsg();
    }

}
